<?php                                                                                  
                                                                                                                                                                                                              
                                                                                    

function multifile_lookup ($subject, $nntp, $count=false, $nzb_arr=false, $custom_pattern = "")                                                                                                                                                                                                                 
{                                                                                             
    global $href, $qs;                                                                                                                                                                                                       
    global $pp_nzbretry;                                                                                                                                                                                                       
    global $pp_nzb_replace;                                                                                                                                                                                                       
    $patterns=array (                                                                                                                                                                                                                 
                       "/[Day]?.*[\[]+(\d+) *\/ *(\d+)[]]+/i",                                                                                                                                                                                                                 
                       "/[Day]?.*[\[](\d+) *- *(\d+)[]]/i",                                                                                                                                                                                                                 
                       "/[Day]?.*[\[\(\s]+(\d+)\s*of\s*(\d+)/i",                                                                                                                                                                                                                 
                       "/[Day]?.*(\d+) *\/ *(\d+)\s*$/i",                                                                                                                                                                                                                 
                       "/[Day]?.*\s+(\d+)\/(\d+)\s+/i",                                                                                                                                                                                                                 
                       "/[Day]?.*[([](\d+) *de *(\d+)[])]/i",                                                                                                                                                                                                                 
                       "/[Day]?.*[([](\d+) *of *(\d+)[])]/i"                                                                                                                                                                                                                  
                    );                                                                                                                                                                                                                 
                                                                                                                                                                                                                     
    if ($custom_pattern!="")                                                                                                                                                                                                                 
    {                                                                                                                                                                                                                 
        $patterns[]=$custom_pattern;                                                                                                                                                                                                                 
    }                                                                                                                                                                                                                                                
    $subject   = preg_replace ("/[(]*[?!]+[)]*/", "", $subject);                                                                                                                                                                                                                  
    foreach ($patterns as $pattern)                                                                                                                                                                                                                 
    {                                                                                                                                                                                                                  
                                                                                                                                                                                                                         
        $reg_tmp = preg_match($pattern, $subject, $tmp);                                                                                                                                                                                                                  
                                                                                                                                                                                                                         
        if ($reg_tmp)                                                                                                                                                                                                                         
        {                                                                                                                                                                                                                 
             $part_string = $tmp[0];                                                                                                                                                                                                                 
             $part_index  = $tmp[1];                                                                                                                                                                                                                 
             if ($tmp[2])                                                                                                                                                                                                                 
             {                                                                                                                                                                                                                 
                 $part_count  = $tmp[2];                                                                                                                                                                                                                 
             }                                                                                                                                                                                                                  
                                                                                                                                                                                                                              
           #  $part_name = substr ($subject, 0, strpos ($subject, $part_string));                                                                                                                                                                                                                   
                                                                                                                                                                                                                              
             if (strlen(trim($part_name))==0)                                                                                                                                                                                                                 
             {                                                                                                                                                                                                                 
                 $part_name   = preg_replace ($pattern, "*" . "\\2", $subject);                                                                                                                                                                                                                   
             }                                                                                                                                                                                                                  
             else                                                                                                                                                                                                                 
             {                                                                                                                                                                                                                 
                 $part_name .= str_replace ($part_index, "*", $part_string);                                                                                                                                                                                                                 
             }                                                                                                                                                                                                                  
             $part_name   = $tmp[0];                                                                                                                                                                                                                   
             $part_name   = str_replace ($part_index, "*", $part_name);                                                                                                                                                                                                                   
                                                                                                                                                                                                                             
             $part_root   = str_replace (" ", "*", $part_name);                                                                                                                                                                                                                 
             $part_root   = str_replace ("[", "*", $part_root);                                                                                                                                                                                                                 
             $part_root   = str_replace ("]", "*", $part_root);                                                                                                                                                                                                                 
                                                                                                                                                                                                                              
             $searches = array (                                                                                                                                                                                                                 
                                "/(\d+)[.](\w+)/",                                                                                                                                                                                                                  
                                "/(\d+)[ \*]*bytes/",                                                                                                                                                                                                                  
                                "/[.][(r)(part)]*(\d+)/",                                                                                                                                                                                                                  
                                "/[(](\d+)\/(\d+)[)]/" ,                                                                                                                                                                                                                  
                                "/(\d+)[ \*]*K/" ,                                                                                                                                                                                                                  
                                "/[\&\"]/" ,                                                                                                                                                                                                                  
                                "/(\d+).*[of|\/]/"                                                                                                                                                                                                                  
                                );                                                                                                                                                                                                                 
                                                                                                                                                                                                                                                 
             $replaces = array (                                                                                                                                                                                                                 
                                "*." . "\\2",                                                                                                                                                                                                                 
                                "*" . "\\2",                                                                                                                                                                                                                 
                                ".*",                                                                                                                                                                                                                 
                                "(*/*)" ,                                                                                                                                                                                                                 
                                "*" . "\\2",                                                                                                                                                                                                                 
                                "*"                                                                                                                                                                                                                 
                               );                                                                                                                                                                                                                 
                                                                                                                                                                                                                              
        #     $part_root = preg_replace ($searches, $replaces, $part_root);                                                                                                                                                                                                                  
             if ($part_index==1 && $part_index==$part_count)                                                                                                                                                                                                                 
             {                                                                                                                                                                                                                 
                 return false;                                                                                                                                                                                                                 
             }                                                                                                                                                                                                                 
             else if (strlen(trim($part_name)) == 0)                                                                                                                                                                                                                 
             {                                                                                                                                                                                                                 
                 return false;                                                                                                                                                                                                                 
             }                                                                                                                                                                                                                 
             else if ($count)                                                                                                                                                                                                                 
             {                                                                                                                                                                                                                 
                 return array ("name"=>$part_root, "root"=>$tmp[0], "count"=>$part_count, "pattern"=>$pattern);                                                                                                                                                                                                                 
             }                                                                                                                                                                                                                 
             else                                                                                                                                                                                                                 
             {  // search the group for matching parts                                                                                                                                                                                                                  
                                                                                                                                                                                                                          
                if (strlen(trim($part_name)) == 0)                                                                                                                                                                                                                 
                {                                                                                                                                                                                                                 
                     return false;                                                                                                                                                                                                                 
                }                                                                                                                                                                                                                 
                                                                                                                                                                                                                                  
                if (is_array($nzb_arr))                                                                                                                                                                                                                 
                {                                                                                                                                                                                                                 
                                                                                                                                                                                                                                 
                }                                                                                                                                                                                                                 
                else                                                                                                                                                                                                                 
                {                                                                                         
                    if (strlen($pp_nzbretry)>0)                                                                                  
                    {                                                                                  
                        $part_root = $pp_nzb_replace;                                                                                  
                    }                                                                                                                                                                                                           
                    raise_event (EVENT_NZB_BEFORE_BEGIN, $part_count, $part_root, $subject);                                                                                                                                                                                                                 
                                                                                                                                                                                                                                     
                    $lookup  = $nntp->search_newsgroup(1, $part_root . "*") ;               
                    if (strlen($_POST["nzmanual"])>0)               
                                                             
                    {                       
                        $form_data = dehydrate ($lookup["articles"], $pattern) ;                                           
                        echo "</form>";               
                        echo "<form method=post action='$href?kl=kl&k=0&" . str_replace('gnzb','slide',$qs) . "'>";                                            
                        foreach ($form_data as $item)                                           
                        {                                          
                            echo "<input name=xnzb[] type=checkbox checked value='" . $item->nntp_message_id . "'>";                                          
                            echo "<input name=nnzb[] size=100 value=\"" . htmlentities($item->subject) . "\"><br>";                                          
                        }                                             
                        echo "<input type=submit value=Export>";                                           
                        echo "</form>";                                                                                                                                                                                 
                        exit;                
                    }                                                  
                                                                                                                                                                            
                  $nzb_arr = array();                                                                                                                                                                                                                    
                    foreach ( $lookup["articles"] as $zb )                                                                                                                                                                                                                 
                    { // load matching ids into an array                                                                                                                                                                                                                 
                        $nzb_arr[] = $zb->nntp_message_id;                                                                                                                                                                                                                   
                    }                                                                                                                                                                                                                   
                                                                                                                                                                                                                                     
                    raise_event (EVENT_NZB_BEGIN, $lookup["articles"]);                                                                                                                                                                                                                 
                }                                                                                                                                  
                # print_r ($nzb_arr);                                                                                                                                                                                             
                $nzb_min = min ($nzb_arr);                                                                                                                                                                                                                 
                $nzb_max = max ($nzb_arr);                                                                                                                                                                                                                   
                                                                                                                                                                                                                                 
                $nzb = search_news ($nntp, $nzb_min, $nzb_max, $nzb_arr, $pattern);                                                                                                                                                                                                                  
                                                                                                                                                                                                                                 
                raise_event (EVENT_NZB_COMPLETE, $nzb["count"], $nzb["bytes"]);                                                                                                                                                                                                                   
                                                                                                                                                                                                                                 
                                                                                                                                                                                                                                 
                return $nzb;                                                                                                                                                                                                                 
             }                                                                                                                                                                                                                   
        }                                                                                                                                                                                                                  
    }                                                                                                                                                                                                                   
    return false;                                                                                                                                                                                                                 
}                                                                                                                                                                                                                 

function dehydrate ($array, $pattern)                                              
{                                              
    $x=0;                                             
    $result = array();                                              
    foreach ($array as $item)                                              
    {                                              
        $x++;                                             
        if (preg_match($pattern, $item->subject, $matches))                                              
        {                                              
            $part_string = $matches[0];                                                                                                                                                                                                                 
            if ( array_key_exists($part_string, $result) === false )                                               
            {                                              
                $result[$part_string] = $item;                                               
            }                                               
        }                                              
    }                                              
    return $result;                                              
}                                              

function search_news ($nntp, $nzb_min, $nzb_max, $nzb_arr, $pattern)                                                                                                                                                                                                                 
{                                                                                                                                                                                                                   
     $unique  = array();                                                                                                                                                                                                                 
     $pieces  = array();                                                                                                                                                                                                                 
     $multis  = array();                                                                                                                                                                                                                 
     $bytes   = array();                                                                                                                                                                                                                 
     $sorter  = array();                                                                                                                                                                                                                  
     $pp_sort = array();                                                                                                                                                                                                                   
                                                                                                                                                                                                                       
     $loop_count = 0;                                                                                                                                                                                                                 
     $fail_count = 0;                                                                                                                                                                                                                 
     $true_count = 0;                                                                                                                                                                                                                 
     for ($nzb_buf=$nzb_min;$nzb_buf<=$nzb_max;$nzb_buf+=15000)                                                                                                                                                                                                                 
     { # 1: -->                                                                                                                                                                                                                
        if ($fail_count > 2)                                                                                                                                                                                                                 
        { # 2: -->                                                                                                                                                                                                                 
            raise_event (EVENT_NZB_ABORT);                                                                                                                                                                                                                   
            break;                                                                                                                                                                                                                 
        } # 2: <!--                                                                                        
        if ($loop_count > 0)                                                                                                                                                                                                                 
        { # 3: -->                                                                                                         
            raise_event (EVENT_NZB_LARGE_FILE, $loop_count . ". Large file: requerying " . $nzb_buf . " ($nzb_max)");                                                                                                                                                                                                                    
        } # 3: <!--                                                                                                                                                                                                                  
        $nzb_diff = $nzb_max - $nzb_min;                                                                                                                                                                                           
        $nzb_this = $nzb_buf + 14999;                                                                                                                                                                                                         
                                                                                                                                                                                                                         
        raise_event (EVENT_NZB_SENDING, $nzb_buf."($nzb_diff)");                                                                                                                                                                                                                  
                                                                                                                                                                                                                          
        // go back and look up all matching headers                                                                                                                                                                                                                  
        $buf = $nntp->send_request("xover " . $nzb_buf . "-" . $nzb_this);                                                                                                                                                                                                                   
        $response   = $nntp->parse_response($buf);                                                                                                                                                                                                                   
                                                                                                                                                                                                                          
        raise_event (EVENT_NZB_RESPONSE_RECEIVED, $buf);                                                                                                                                                                                                                  
                                                                                                                                                                                                                         
        $multipart_arr = array ();                                                                                                                                                                                                                   
        $line_count = 0;                                                                                                                                                                                                                 
        $item_count = 0;                                                                                                                                                                                                                 
        if ($response["status"] == ARTICLE_OVERVIEW)                                                                      
        { # 4: -->                                                                                                                                                                                                                  
            $buf = fgets($nntp->nntp, 4096);                                                                                                                                                                                                                  
            while (!preg_match("/^\.\s*$/", $buf))                                                                      
            { # 5: -->                                                                                                                                                                                                                  
                $elements = preg_split("/\t/", $buf);                                                                                                                                                                                                                   
                $elements[1] = decode_MIME_header($elements[1]);    // Decode subject                                                                                                                                                                                                                  
                $elements[2] = decode_MIME_header($elements[2]);    // Decode from                                                                                                                                                                                                                   
                                                                                                                                                                                                                                 
                $message_info = new MessageInfo();                                                                                                                                                                                                                  
                $message_info->nntp_message_id = $elements[0];                                                                                                                                                                                                                  
                if ( array_search($message_info->nntp_message_id, $nzb_arr) !== false )                                                                                                                                                                                                                 
                { // 6: --> make sure this article is in the list                                                                                                                                                                                                                 
                   $item_count ++;                                                                                                                                                                                                                 
                    $message_info->subject = $elements[1];                                                                                                                                                                                                                  
                    $message_info->from = decode_sender($elements[2]);                                                                                                                                                                                                                  
                    $message_info->date = strtotime($elements[3]);                                                                                                                                                                                                                  
                    if ($message_info->date == -1)                                                                      
                    { # 7: -->                                                                                                                                                                                                                  
                        $message_info->date = $elements[3];                                                                                                                                                                                                                  
                    } # 7: <!--                                                                                                                                                                                                                   
                    $message_info->message_id = $elements[4];                                                                                                                                                                                                                  
                    if (strlen($elements[5]) != 0)                                                                      
                    { # 8: -->                                                                                                                                                                                                                 
                        $message_info->references = preg_split("/\s+/", trim($elements[5]));                                                                                                                                                                                                                  
                    } else # 8: <!--                                                                       
                    { # 9: -->                                                                                                                                                                                                                  
                        $message_info->references = array();                                                                                                                                                                                                                  
                    } # 9: <!--                                                                                                                                                                                                                   
                    $message_info->byte_count = $elements[6];                                                                                                                                                                                                                  
                    $message_info->line_count = $elements[7];                                                                                                                                                                                                                  
                                                                                                                                                                                                                                     
                    if ($pattern == "")                                                                                                                                                                                                                 
                    { # 10: -->                                                                                                                                                                                                                  
                         $pieces[$line_count] = $message_info;                                                                                                                                                                                                                 
                         $sorter[$line_count] = $line_count;                                                                                                                                                                                                                 
                         $multis[$line_count] = array($message_info);                                                                                                                                                                                                                 
                    } # 10: <!--                                                                                                                                                                                                                 
                    else                                                                                                                                                                                                                  
                    { # 11: -->                                                                                                                                                                                                                 
                         $reg_match = preg_match($pattern, $message_info->subject, $matches);                                                                                                                                                                                                                  
                                                                                                                                                                                                                                      
                         if ($reg_match)                                                                                                                                                                                                                     
                         {  # 12: -->                                                                                                                                                                                                                 
                             $part_string = $matches[0];                                                                                                                                                                                                                 
                             $part_index  = $matches[1];                                                                                                                                                                                                                 
                             $part_count  = $matches[2];                                                                                                                                                                                                                 
                                                                                                                                                                                                                                              
                                                                                                                                                                                                                                          
                             $pp_index  = 0;                                                                                                                                                                                                                 
                             $pp_count  = 0;                                                                                                                                                                                                                 
                             $reg_tmp = preg_match("/[(](\d+)\/(\d+)[)]/", $message_info->subject, $tmp);                                                                                                                                                                                                                 
                             if ($reg_tmp)                                                                                                                                                                                                                 
                             { # 13: -->                                                                                                                                                                                                                 
                                 $pp_index  = $tmp[1];                                                                                                                                                                                                                 
                                 $pp_count  = $tmp[2];                                                                                                                                                                                                                 
                             } # 13: <!--                                                                                                                                                                                                                  
                                                                                                                                                                                                                         
                             if ( array_key_exists($part_string, $multis) )                                                                                                                                                                                                                 
                             {  # 14: -->                                                                                                                                                                                                                
                                 if (array_search ($message_info->subject, $unique[$part_index])===false)                                                                                                                                                                                                                 
                                 { # 15: -->                                                                                                                                                                                                                 
                                     $multis[$part_string][ count ($multis[$part_string]) ] = $message_info;                                                                                                                                                                                                                   
                                     $pp_sort[$part_index][ count ($pp_sort[$part_index]) ] = $message_info->subject;                                                                                                                                                                                                                 
                                     $bytes[$part_string] += $message_info->byte_count;                                                                                                                                                                                                                  
                                     $part_label           = str_replace(" ", "_", $part_string);                                                                                                                                                                                                                 
                                                                                                                                                                                                                                                      
                                    // progress                                                                                                                                                                                                                 
                                    // ---------------------------------------------------------------------'                                                                                                                                                                                                                 
                                     $pp_index = count ($multis[$part_string]);                                                                                                                                                                                                                 
                                     $percent =  100 * ($pp_index/$pp_count);                                                                                                                                                                                                                 
                                                                                                                                                                                                                                                      
                                     if ($percent>100)                                                                                                                                                                                                                 
                                     { # 16: -->                                                                                                                                                                                                                 
                                        $percent=100;                                                                                                                                                                                                                 
                                     } # 16: <!--                                                                                                                                                                                                                 
                                                                                                                                                                                                                                                      
                                                                                                                                                                                                                                                      
                                     raise_event (EVENT_NZB_PROGESS, $part_label , htmlentities( chop_str($message_info->subject, 140) ),                                                                                                                                                                                                                  
                                                 $pp_index . "/" . $pp_count, $bytes[$part_string], $percent, $message_info->nntp_message_id );                                                                                                                                                                                                                  
                                                                                                                                                                                                                                                       
                                                                                                                                                                                                                                                                                                                                                                                      
                                     if ($pp_index==$pp_count)                                                                                                                                                                                                                 
                                     { # 17: -->                                                                                                                                                                                                                  
                                         raise_event (EVENT_NZB_ITEM_DOWNLOADED, $part_label );                                                                                                                                                                                                                  
                                                                                                                                                                                                                                                          
                                         $true_count ++;                                                                                                                                                                                                                 
                                                                                                                                                                                                                                                          
                                         // sort segments by INDEX                                                                                                                                                                                                                 
                                         sort ($pp_sort[$part_index]);                                                                                                                                                                                                                 
                                         $sorted_multi = array ();                                                                                                                                                                                                                 
                                         foreach ($pp_sort[$part_index] as $pp_s)                                                                                                                                                                                                                 
                                         { # 18: -->                                                                                                                                                                                                                 
                                             for ($r=0;$r<count($multis[$part_string]);$r++)                                                                                                                                                                                                                 
                                             { # 19: -->                                                                                                                                                                                                                 
                                                if ($multis[$part_string][$r]->subject==$pp_s)                                                                                                                                                                                                                 
                                                { # 20: -->                                                                                                                                                                                                                 
                                                    $sorted_multi[count($sorted_multi)] = $multis[$part_string][$r];                                                                                                                                                                                                                 
                                                    break;                                                                                                                                                                                                                 
                                                } # 20: <!--                                                                                                                                                                                                                 
                                             } # 19: <!--                                                                                                                                                                                                                 
                                         } # 18: <!--                                                                                                                                                                                                                 
                                        $multis[$part_string] = $sorted_multi;                                                                                                                                                                                                                  
                                     } # 17: <!--                                                                                                                                                                                                                 
                                     if ($pp_index>$pp_count)                                                                                                                                                                                                                 
                                     { # 21: -->                                                                                                                                                                                                                 
                                         raise_event (EVENT_NZB_TOO_MANY_ITEMS, $part_label);                                                                                                                                                                                                                   
                                     } # 21: <!--                                                                                                                                                                                                                 
                                    // ---------------------------------------------------------------------'                                                                                                                                                                                                                  
                                 } # 15: <!--                                                                                                                                                                                                                   
                                 $unique[$part_index][ count ($unique[$part_index]) ] = $message_info->subject;                                                                                                                                                                                                                     
                             } # 14: <!--                                                                                                                                                                                                                 
                             else                                                                                                                                                                                                                 
                             { # 22: -->                                                                                                                                                                                                                  
                                 $pieces[$part_index]   = $message_info;                                                                                                                                                                                                                 
                                 $sorter[$part_string]  = $part_string;                                                                                                                                                                                                                 
                                 $unique[$part_index]   = array($message_info->subject);                                                                                                                                                                                                                 
                                 $pp_sort[$part_index]  = array($message_info->subject);                                                                                                                                                                                                                 
                                 $multis[$part_string]  = array($message_info);                                                                                                                                                                                                                  
                                 $bytes[$part_string]   = $message_info->byte_count;                                                                                                                                                                                                                 
                                 $part_label            = str_replace(" ", "_", $part_string);                                                                                                                                                                                                                 
                                                                                                                                                                                                                                                  
                                 if ($pp_count==1|| !$reg_tmp)                                                                                                                                                                                                                 
                                 { # 23: -->                                                                                                                                                                                                                  
                                     $true_count ++;                                                                                                                                                                                                                 
                                 } # 23: <!--                                                              
                                                                                                                                                                                                                  
                                 // progress                                                                                                                                                                                                                 
                                 // ---------------------------------------------------------------------'                                                                                                                                                                                                                 
                                 $bgcolor =  $pp_count==1 || !$reg_tmp?"#E0FFE0":"mistyrose";                                                                                                                                                                                                                 
                                 $width   =  $pp_count==1 || !$reg_tmp?100:100*(1/$pp_count);                                                                                                                                                                                                                 
                                                                                                                                                                                                                                                   
                                                                                                                                                                                                                                                  
                                 raise_event ( EVENT_NZB_ITEM_DOWNLOADING, $part_label, $bgcolor, $width,                                                                                                                                                                                                                 
                                               htmlentities(substr($message_info->subject,0,100)), $bytes[$part_string],                                                                                                                                                                                                                  
                                               date("Y-m-d", $message_info->date), $message_info->nntp_message_id );                                                                                                                                                                                                                   
                                  // ---------------------------------------------------------------------'                                                                                                                                                                                                                   
                             } # 22: <!--                                                                                                                                                                                                                   
                         } # 12: <!--                                                                                                                                                                                                                   
                     } # 11: <!--                                                                                                                                                                                                                  
                     $line_count ++;                                                                                                                                                                                                                 
                 } # 6: <!--                                                                                                                                                                                                                  
                $buf = fgets($nntp->nntp, 4096);                                                                                                                                                                                                                  
            } # 5: <!--                                                                                                                                                                                                                  
        } # 4: <!--                                                               
                                                                                                                                                                                                                  
        if ($item_count==0)                                                                                                                                                                                                                 
        { # 24: -->                                                                                                                                                                                                                 
            $fail_count ++;                                                                                                                                                                                                                 
        } # 24: <!--                                                                                              
                                                                                                    
        if ($nzb_buf < $nzb_max)                                                                             
        { # 25: -->                                                                                                                                        
            raise_event (EVENT_NZB_LARGE_FILE, "Large file: requerying " . $nzb_buf . " ($nzb_max)");                                                                              
        } # 25: <!--                                                                                   
        $loop_count ++;                                                                                                                                                                                             
    } # 1: <!--                                                             
                                                                                                                                                                                                                  
    sort ($sorter);                                                                                                                                                                                                                 
                                                                                                                                                                                                                      
    if ($true_count < $part_count)                                                                                                                                                                                                                 
    { # 26: -->                                                                                                                                                                                                                 
        raise_event ( EVENT_NZB_PARTIAL_DOWNLOAD, $true_count,  $part_count);                                                                                                                                                                                                                 
    } # 26: <!--                                                                                                                                                                                                                 
                                                                                                                                                                                                                  
    return array("matches"=>$pieces,                                                                                                                                                                                                                 
              "sorter"=>$sorter,                                                                                                                                                                                                                 
              "multis"=>$multis,                                                                                                                                                                                                                 
              "bytes"=>array_sum($bytes),                                                                                                                                                                                                                 
              "count"=>$part_count);                                                                                                                                                                                                                   
}                                                                                                                                                                                                                 



function multipart_lookup ($subject, $nntp, $count=false)                                                                                                                                                                                                                 
{                                                                                                                                                                                                                  
    $reg_tmp = preg_match("/[(](\d+)\/(\d+)[)]/", $subject, $tmp);                                                                                                                                                                                                                  
    if ($reg_tmp)                                                                                                                                                                                                                         
    {                                                                                                                                                                                                                 
         $part_string = $tmp[0];                                                                                                                                                                                                                 
         $part_index  = $tmp[1];                                                                                                                                                                                                                 
         $part_count  = $tmp[2];                                                                                                                                                                                                                 
                                                                                                                                                                                                                          
         $part_root   = preg_replace ("/[(](\d+)\/(\d+)[)]/", "(*/" . "\\2" . ")", $subject); # substr ($subject, 0, $char_pos_string);                                                                                                                                                                                                                 
         $part_root   = str_replace (" ", "*", $part_root);                                                                                                                                                                                                                 
         $part_root   = str_replace ("[", "*", $part_root);                                                                                                                                                                                                                 
         $part_root   = str_replace ("]", "*", $part_root);                                                                                                                                                                                                                 
                                                                                                                                                                                                                          
                                                                                                                                                                                                                          
        # print $part_root;                                                                                                                                                                                                                 
                                                                                                                                                                                                                          
         if (strlen(trim($part_root)) == 0)                                                                                                                                                                                                                 
         {                                                                                                                                                                                                                 
             return false;                                                                                                                                                                                                                 
         }                                                                                                                                                                                                                 
         /*else if ($part_index==0)                                                                                                                                                                                                                 
         {                                                                                                                                                                                                                 
              return false;                                                                                                                                                                                                                 
         } */                                                                                                                                                                                                                
         else if ($part_index==1 && $part_index==$part_count)                                                                                                                                                                                                                 
         {                                                                                                                                                                                                                 
             return false;                                                                                                                                                                                                                 
         }                                                                                                                                                                                                                 
         else if ($part_count < $part_index)                                                                                                                                                                                                                 
         {                                                                                                                                                                                                                 
             return false;                                                                                                                                                                                                                 
         }                                                                                                                                                                                                                 
         else if ($count)                                                                                                                                                                                                                 
         {                                                                                                                                                                                                                 
             return $part_count;                                                                                                                                                                                                                 
         }                                                                                                                                                                                                                 
         else                                                                                                                                                                                                                 
         {  // search the group for matching parts                                                                                                                                                                                                                 
             $lookup = $nntp->search_newsgroup(1, $part_root . "*") ;                                                                                                                                                                                                                 
             $nzb_unq = array();                                                                                                                                                                                                                 
             $pieces  = array();                                                                                                                                                                                                                 
             $sorter  = array();                                                                                                                                                                                                                  
             # print_r ( $lookup);                                                                                                                                                                                                                 
             # exit;                                                                                                                                                                                                                 
             foreach ($lookup["articles"] as $l)                                                                                                                                                                                                                 
             {                                                                                                                                                                                                                  
               # print $l->subject . "<br>";                                                                                                                                                                                                                 
                 $reg_match = preg_match("/[(](\d+)\/(\d+)[)]/", $l->subject, $matches);                                                                                                                                                                                                                  
                if ($reg_match)                                                                                                                                                                                                                     
                 {                                                                                                                                                                                                                 
                     $part_string = $matches[0];                                                                                                                                                                                                                 
                     $part_index  = $matches[1];                                                                                                                                                                                                                 
                     $part_count  = $matches[2];                                                                                                                                                                                                                  
                     if ($part_index > 0 && ! array_key_exists($part_index, $pieces))                                                                                                                                                                                                                 
                     {                                                                                                                                                                                                                  
                         if (array_search($l->subject,$nzb_unq)===false)                                                                                                                                                                                                                 
                         {                                                                                                                                                                                                                 
                             $pieces[$part_index] = $l;                                                                                                                                                                                                                 
                             $sorter[$part_index] = $part_index;                                                                                                                                                                                                                 
                             array_push ($nzb_unq, $l->subject);                                                                                                                                                                                                                 
                         }                                                                                                                                                                                                                 
                     }                                                                                                                                                                                                                  
                 }                                                                                                                                                                                                                 
             }                                                                                                                                                                                                                  
             sort ($sorter);                                        
                                                                                                                                                                                                                 
             return array("matches"=>$pieces,"sorter"=>$sorter);                                                                                                                                                                                                                   
         }                                                                                                                                                                                                                  
         exit;                                                                                                                                                                                                                 
    }                                                                                                                                                                                                                  
    else                                                                                                                                                                                                                 
    {                                                                                                                                                                                                                 
       return false;                                                                                                                                                                                                                 
    }                                                                                                                                                                                                                   
}                                                                                                                                                                                                                 

function cmp_by_article($msg1, $msg2)                       
{                                                                                     
    $reg_match1 = preg_match("/[(](\d+)\/(\d+)[)]/", $msg1["subject"], $msg1_matches);                       
    $reg_match2 = preg_match("/[(](\d+)\/(\d+)[)]/", $msg2["subject"], $msg2_matches);                      
    if (reg_match1 && reg_match2)                      
    {                      
        return $msg1_matches[1] < $msg2_matches[1] ? -1 : 1;                      
    }                                                                                                                                                                                    
    return 0;                      
}                         

                                                                                                                                                                                                                      

function gnzb_create($nntp, $message, $nzb_arr=false, $custom="")                                                                                                                                                                                                                
{                                                                                                                                                                                                                                                                         
    $header  = $message->get_main_header();                                                                                                                                                                                                                 
    $parts   = $message->get_all_parts();                                                                                                                                                                                                                  
    $gnzb    = create_nzb_header();                                                                                                                                                                                                                 
                                                                                                                                                                                                                     
    $nzb_unq = array();                                                                                                                                                                                                                   
    $nzb     = multifile_lookup ($header["subject"], $nntp, false, $nzb_arr, $custom) ;                                                                                                                                                                                                                
                                                                                                                                                                                                                      
    if ($nzb_arr && count($nzb["multis"])==0)                                                                                                                                                                                                                
    {                                                                                                                                                                                                                 
        $nzb = search_news ($nntp, min($nzb_arr), max($nzb_arr), $nzb_arr, "");                                                                                                                                                                                                                 
    }                                                                                                                                                                                                                
                                                                                                                                                                                                                      
     if (count($nzb["sorter"])==0)                                                                                                                                                                                                                
     {                                                                                                                                                                                                                
        print "No matches found for ".$header["subject"];                                                                                                                                                                                                                
     }                                                                                                                                                                                                                 
                                                                                                                                                                                                                     
    foreach ( $nzb["sorter"] as $zs )                                                                                                                                                                                                                
    {                                                                                                                                                                                                                   
                                                                                                                                                                                                                        
        $zb    = $nzb["multis"][$zs];                                                                                                                                                                                                                
                                                                                                                                                                                                                        
        raise_event (EVENT_NZB_WRITING_FILE, $zs );                                                                                                                                                                                                                
                                                                                                                                                                                                                        
        $gnzb .= '  <file poster="' . htmlentities($zb[0]->from["name"] )                                                                                                                                                                                                                
                   . '" date="' . $zb[0]->date                                                                                                                                                                                                                
                   . '" subject="' . htmlentities($zb[0]->subject) . '">' . chr(13);                                                                                                                                                                                                                

        $gnzb .= '  <groups>' . chr(13);                                                                                                                                                                                                                
        $gnzb .=  '    <group>' . get_request("art_group") . '</group>' . chr(13);                                                                                                                                                                                                                
        $gnzb .= '  </groups>' . chr(13);                                                                                                                                                                                                                
                                                                                                                                                                                                                        
        $gnzb .= '  <segments>' . chr(13);                                                                                                                                                                                                                    
                                                                                                                                                                                                                        
        $segment = 1;                                                                                                                                                                                                                
        foreach ($zb as $nz )                                                                                                                                                                                                                
        {                                                                                                                                                                                                                 
            if (array_search($nz->subject,$nzb_unq)===false)                                                                                                                                                                                                                
            {                                                                                                                                                                                                                
                                                                                                                                                                                                                                
                 raise_event (EVENT_NZB_WRITING_SEGMENT, $zs, $nz->nntp_message_id );                                                                                                                                                                                                                
                                                                                                                                                                                                                                 
                $msg_id     = $nz->message_id;                                                                                                                                                                                                                 
                $msg_id     = str_replace("<", "", $msg_id);                                                                                                                                                                                                                 
                $msg_id     = str_replace(">", "", $msg_id);                                                                                                                                                                                                                 
                $gnzb .= '    <segment bytes="' . $nz->byte_count . '" number="' . $segment . '" id="' . $nz->nntp_message_id .'">' .  htmlentities($msg_id) . '</segment>' . chr(13);                                                                                                                                                                                                                 
                array_push($nzb_unq, $nz->subject);                                                                                                                                                                                                                
                $segment ++;                                                                                                                                                                                                                
            }                                                                                                                                                                                                                
        }                                                                                                                                                                                                                  
        $gnzb .= '  </segments>' . chr(13);                                                                                                                                                                                                                  
        $gnzb .= "</file>" . chr(13);                                                                                                                                                                                                                   
    }                                                                                                                                                                                                                   
                                                                                                                                                                                                                    
    $gnzb .= "</nzb>" . chr(13);                                                                                                                                                                                                                   
    return $gnzb;                                                                                                                                                                                                                
}                                                                                   


function nzb_create($nntp, $message, $useheader=true)                                                                                                                                                                                                                
{                                                                                                                                                                                                                

    $segment = 1;                                                                                                                                                                                                                 
                                                                                                                                                                                                                    
    $header  = $message->get_main_header();                                                                                                                                                                                                                    
    $subject = $header["subject"];                                                                                                                                                                                                                 
                                                                                    
    $nzb = "";                                                                             
    if ($useheader)                                                                           
    {                                                                                                                                                                                                         
        // add generic header                                                                                                                                                                                                                
        $nzb .= create_nzb_header();                                                                                                                                                                                                                 
    }                                                                           
                                                                                                                                                                                                                
    // add file marker                                                                                                                                                                                                                
    $nzb .= create_nzb_file_marker($message);                                                                                                                                                                                                                 
                                                                                                                                                                                                                    
    // lookup file segments                                                                                                                                                                                                                
    $multi_test = multipart_lookup ($subject, $nntp);                                                                                                                                                                                                                 
                                                                                                                                                                                                                    
    $nzb .= '  <segments>' . chr(13);                                                                                                                                                                                                                  
                                                                                                                                                                                                                    
    $encode   = "";                                                                                                                                                                                                                
    $count    = 0;                                                                                                                                                                                                                 
                                                                                                                                                                                                                    
    $cont     = "";                                                                                                                                                                                                                
                                               
    $matchkey = "";                                                                                                                                                                                                                
                                               
    if ( preg_match("/(.*)[(](\d+)\/(\d+)[)]/", $subject, $matches) )                        
    {                        
        $matchkey = $matches[1];                        
    }                        
    #echo $matchkey . "<br>---------<br>";                        
                
    if (count($multi_test["matches"]) > 0)               
    {               
        $nzb_arr = array();                                                                                                                                                                                                                   
        foreach ( $multi_test["matches"] as $zb )                                                                                                                                                                                                                
        { // load matching ids into an array                                                                                                                                                                                                                
            $nzb_arr[] = $zb->nntp_message_id;                                                                                                                                                                                                                  
        }                                                                                                                                                                                                                  
                                                                                                                                              
                                                                                                                                                                                       
        $nzb_min = min ($nzb_arr);                                                                                                                                                                                                                
        $nzb_max = max ($nzb_arr);                                                                                                                                                                                                                  
                                                                         
                                
        #print ($nzb_min) . "<br>";                                
        #print ($nzb_max) . "<br>";                     
                           
        $z=$nntp->get_summary($nzb_min, $nzb_max);                        
        uasort ($z, cmp_by_article);                            
        foreach ($z as $xx)                         
        {                         
            if ($matchkey=="" || strpos($xx["subject"], $matchkey)!==false)                       
            {                       
                #echo $xx["subject"] . "<br>";                  
                $seg   = create_article_segment($xx, $segment);                     
                                                                                                                                                                                                          
                if (strlen($seg) > 0)                                                                                                                                                                                                                
                {                                                                                                                                                                                                                
                    $segment ++;                                                                                                                                                                                                                
                    $nzb .= $seg;                                                                                                                                                                                                                
                }                   
            }                       
                                 
        }               
    }               
    else               
    {               
          $nzb .= create_article_segment($header, 1);                 
    }                                
                           
/*                 
                                                                                                                                                                               
    foreach ($multi_test["sorter"] as $sorter)                                                                                                                                                                                                                 
    {                                                                                                                                                                                                                    
       $match = $multi_test["matches"][$sorter];                                                                                                                                                                                                                  
       $seg   = create_nzb_segment($nntp, $match->nntp_message_id, $segment);                                                                                                                                                                                                                

                                                                                                                                                                                                          
       if (strlen($seg) > 0)                                                                                                                                                                                                                
       {                                                                                                                                                                                                                
           $segment ++;                                                                                                                                                                                                                
           $nzb .= $seg;                                                                                                                                                                                                                
       }                                                                                                                                                                                                                  
    }                       
 */                                                                                                                                                                                                         
    $nzb .= '  </segments>' . chr(13);                                                                                                                                                                                                                 
    #exit;                                                                                                                                                                                                                
                                                                                                                                                                                                                    
                                                                                                                                                                                                                    
                                                                                                                                                                                                                    
    // footer                                                                                                                                                                                                                
    $nzb .= '  </file>' . chr(13);                                                                               
                                                                                          
    if ($useheader)                                                                           
    {                                                                                                                                                                                              
        $nzb .= '</nzb>' . chr(13);                                                                                                                                                                                                                
                                                                                                                                                                                                                    
    }                                                                           
    return $nzb;                                                                                                                                                                                                                 
}                                                                                                                                                                                                                


function create_nzb_header()                                                                                                                                                                                                                
{                                                                                                                                                                                                                
    $nzb = '<' . '?xml version="1.0" encoding="iso-8859-1" ?' . '> ' . chr(13);                                                                                                                                                                                                                
    $nzb .= '<!DOCTYPE nzb PUBLIC "-//newzBin//DTD NZB 1.0//EN" "http://www.newzbin.com/DTD/nzb/nzb-1.0.dtd">' . chr(13);                                                                                                                                                                                                                
    $nzb .= '<nzb xmlns="http://www.newzbin.com/DTD/2003/nzb">' . chr(13);                                                                                                                                                                                                                
                                                                                                                                                                                                                    
    $nzb = '<' . '?xml version="1.0" encoding="iso-8859-1" ?' . '> ' . chr(13);                                                                                                                                                                                                                 
    $nzb .= '<nzb>' . chr(13);                                                                                                                                                                                                                
    return $nzb;                                                                                                                                                                                                                
}                                                                                                                                                                                                                



function create_nzb_file_marker($message)                                                                                                                                                                                                                
{                                                                                                                                                                                                                
    $header       = $message->get_main_header();                                                                                                                                                                                                                   
    $subject      = $header["subject"];                                                                                                                                                                                                                 
    $from         = $header["from"];                                                                                                                                                                                                                 
    $date         = $header["date"];                                                                                                                                                                                                                  
    $groups       = explode(',', $header["newsgroups"]);                                                                                                                                                                                                                  
    $subject      = str_replace ('"','\'', $subject);                                                                                                                                                                                                                 
    $from["name"] = str_replace ('"','\'', $from["name"]);                                                                                                                                                                                                                 
                                                                                                                                                                                                                    
    $nzb  = '  <file poster="' . htmlentities( utf8_encode($from["name"]) ) . '" date="' . $date . '" subject="' . htmlentities( utf8_encode($subject) ) . '">' . chr(13);                                                                                                                                                                                                                
                                                                                                                                                                                                                    
    $nzb .= '  <groups>' . chr(13);                                                                                                                                                                                                                
    foreach ($groups as $group)                                                                                                                                                                                                                 
    {                                                                                                                                                                                                                 
        $nzb .=  '    <group>' . $group . '</group>' . chr(13);                                                                                                                                                                                                                
    }                                                                                                                                                                                                                 
    $nzb .= '  </groups>' . chr(13);                                                                                                                                                                                                                
                                                                                                                                                                                                                    
                                                                                                                                                                                                                    
    return $nzb;                                                                                                                                                                                                                
}                                                                                                                                                                                                                


function create_article_segment($message, $segment)                                                                                                                                                                                                                
{                                                                                                                                                                                   
    $msg_id    = str_replace("<", "", $message["message_id"]);                                                                                                                                                                                                                 
    $msg_id    = str_replace(">", "", $msg_id);                                                                                                                                                                                                  
   $nzb = '    <segment bytes="' . $message["byte_count"] . '" number="' . $segment . '">' .  htmlentities($msg_id) . '</segment>' . chr(13);                                                                                                                                                                                                                                                                                                                                                                                                         
   return $nzb;                                                                                                                                                                                                                
}                                                                                                                                                                                                                




function create_nzb_segment($nntp, $message_id, $segment)                                                                                                                                                                                                                
{                                                                                                                                                                                                                
    // call GET_MESSAGE_SUMMARY to get BYTE_COUNT                                                                                                                                                                                                                
   $tmp_msg = $nntp->get_message_summary($message_id, floor($message_id) + 1);                                                                                                                                                                                                                 
   $nzb = "";                                                                                                                                                                                                                
   if (count($tmp_msg["articles"]) > 0)                                                                                                                                                                                                                
   {                                                                                                                                                                                                                 
       $tmp_msg   = $tmp_msg["articles"][0];                                                                                                                                                                                                                
       $msg_id    = $tmp_msg->nntp_message_id;                                                                                                                                                                                                                 
       $msg_size  = $tmp_msg->byte_count;                                                                                                                                                                                                                 
       if (isset ($_POST["xnzb"]) || isset ($_POST["cnzb"]))                                                                                            
       {                                      
           echo " Creating segment: " . $tmp_msg->subject . "<br>";                                                
       }                                          
                                                                                                                                                                                                               
                                                                                                                                                                                                                     
       // MESSAGE-ID usually returns blank but try anyways                                                                                                                                                                                                                
       $msg_tmp   = $tmp_msg->message_id;                                                                                                                                                                                                                 
       if (strlen($msg_tmp) > 0)                                                                                                                                                                                                                
       {                                                                                                                                                                                                                
           $msg_id    = $msg_tmp;                                                                                                                                                                                                                
       }                                                                                                                                                                                                                
       else                                                                                                                                                                                                                
       {// call GET_ARTICLE to get MESSAGE-ID                                                                                                                                                                                                                
           $tmp_msg   = $nntp->get_article($msg_id);                                                                                                                                                                                                                 
           $tmp_hdr   = $tmp_msg->get_main_header();                                                                                                                                                                                                                  
           $msg_id    = $tmp_hdr["message-id"];                                                                                                                                                                                                                 
       }                                                                                                                                                                                                                
       $msg_id    = str_replace("<", "", $msg_id);                                                                                                                                                                                                                 
       $msg_id    = str_replace(">", "", $msg_id);                                                                                                                                                                                                                 
               # s="' . htmlentities($tmp_hdr["subject"]) . '"                                                                                                                                                                                                                
       $nzb .= '    <segment bytes="' . $msg_size . '" number="' . $segment . '">' .  htmlentities($msg_id) . '</segment>' . chr(13);                                                                                                                                                                                                                 
   }                                                                                                                                                                                                                 
   else                                                                                                                                                                                                                
   {                                                                                                                                                                                                                 
       $nzb .= "Message not found ".$message_id;                                                                                                                                                                                                                
       $nzb .= "\n<br>\n[" . $nntp->error_message . "]";                                                                                                                                                                                                                
   }                                                                                                                                                                                                                
   return $nzb;                                                                                                                                                                                                                
}                                                                                                                                                                                                                




?>